import type { CustomerForm, CustomerQuery, CustomerVO } from './model';

import type { ID, IDS, PageResult } from '#/api/common';
import type { UserImportParam } from '#/api/system/user/model';

import { commonExport, ContentTypeEnum } from '#/api/helper';
import { requestClient } from '#/api/request';

/**
 * 查询客户信息列表
 * @param params
 * @returns 客户信息列表
 */
export function customerList(params?: CustomerQuery) {
  return requestClient.get<PageResult<CustomerVO>>('/hm/customer/list', {
    params,
  });
}

/**
 * 下载客户导入模板
 * @returns blob
 */
export function downloadImportCustomerTemplate() {
  return requestClient.post<Blob>(
    '/hm/customer/importTemplate',
    {},
    {
      isTransformResponse: false,
      responseType: 'blob',
    },
  );
}

/**
 * 从excel导入用户
 * @param data
 * @returns void
 */
export function customerImportData(data: UserImportParam) {
  return requestClient.post<{ code: number; msg: string }>(
    'hm/customer/importData',
    data,
    {
      headers: {
        'Content-Type': ContentTypeEnum.FORM_DATA,
      },
      isTransformResponse: false,
      timeout: 30 * 1000,
    },
  );
}

/**
 * 导出客户信息列表
 * @param params
 * @returns 客户信息列表
 */
export function customerExport(params?: CustomerQuery) {
  return commonExport('/hm/customer/export', params ?? {});
}

/**
 * 查询客户信息详情
 * @param customerId id
 * @returns 客户信息详情
 */
export function customerInfo(customerId: ID) {
  return requestClient.get<CustomerVO>(`/hm/customer/${customerId}`);
}

/**
 * 新增客户信息
 * @param data
 * @returns void
 */
export function customerAdd(data: CustomerForm) {
  return requestClient.postWithMsg<void>('/hm/customer', data);
}

/**
 * 更新客户信息
 * @param data
 * @returns void
 */
export function customerUpdate(data: CustomerForm) {
  return requestClient.putWithMsg<void>('/hm/customer', data);
}

/**
 * 更新家庭组信息
 * @param data
 * @returns void
 */
export function customerUpdateFamily(data: CustomerForm) {
  return requestClient.putWithMsg<void>('/hm/customer/updateFamily', data);
}

/**
 * 删除客户信息
 * @param customerId id
 * @returns void
 */
export function customerRemove(customerId: ID | IDS) {
  return requestClient.deleteWithMsg<void>(`/hm/customer/${customerId}`);
}

/**
 * 获取指定销售人员名下的所有客户信息
 * @param staffId
 * @returns 客户信息
 */
export function customerListByStaffId(staffId: ID) {
  return requestClient.get<CustomerVO[]>(`/hm/customer/list/${staffId}`);
}

/**
 * 获取指定销售人员名下的所有客户信息
 * @param staffId
 * @returns 客户信息
 */
export function customerListByStaffIds(staffIds: []) {
  return requestClient.postWithMsg<CustomerVO[]>(
    `/hm/customer/list/staffId`,
    staffIds,
  );
}

/**
 * 获取指定客户下的购买、提货详情
 * @param customerId
 * @returns 客户信息
 */
export function customerDetail(customerId: ID) {
  return requestClient.get<CustomerVO>(
    `/hm/customer/${customerId}/purchaseRecord`,
  );
}

/**
 * 未加入家庭组的客户
 * @param params
 * @returns void
 */
export function familyUnallocatedList(params: any) {
  return requestClient.get<PageResult<CustomerVO>>(
    '/hm/customer/family/unallocatedList',
    {
      params,
    },
  );
}

/**
 * 加入家庭组的客户
 * @param params
 * @returns void
 */
export function familyAllocatedList(params: any) {
  return requestClient.get<PageResult<CustomerVO>>(
    '/hm/customer/family/locatedList',
    {
      params,
    },
  );
}
